package com.qzdsoft.erpcloud.service.pu.impl;

import java.util.ArrayList;
import java.util.Date;
import java.util.List;

import org.apache.commons.collections4.CollectionUtils;
import org.slf4j.Logger;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import com.qzdsoft.erpcloud.domain.pu.PurchaseOrderDetail;
import com.qzdsoft.erpcloud.mapper.com.CustomerSupplierStatementQueryMapper;
import com.qzdsoft.erpcloud.mapper.com.SupplierCustomerMapper;
import com.qzdsoft.erpcloud.mapper.proj.MaterialQueryMapper;
import com.qzdsoft.erpcloud.mapper.pu.PurchaseOrderDetailMapper;
import com.qzdsoft.erpcloud.mapper.pu.PurchaseOrderDetailQueryMapper;
import com.qzdsoft.erpcloud.mapper.pu.PurchaseOrderMapper;
import com.qzdsoft.erpcloud.service.pu.PurchaseOrderDetailService;
import com.qzdsoft.erpcloud.vo.proj.MaterialInfo;
import com.qzdsoft.erpcloud.vo.pu.PurchaseOrderDetailEditInfo;
import com.qzdsoft.erpcloud.vo.pu.PurchaseOrderDetailEditItemInfo;
import com.qzdsoft.erpcloud.vo.pu.PurchaseOrderDetailInfo;
import com.qzdsoft.erpcloud.vo.pu.PurchaseOrderDetailQueryInfo;
import com.qzdsoft.utils.StringUtil;
import com.qzdsoft.vo.Response;
import com.qzdsoft.vo.ResultEnum;

@Service
public class PurchaseOrderDetailServiceImpl implements PurchaseOrderDetailService {

    private static final Logger logger = org.slf4j.LoggerFactory.getLogger(PurchaseOrderDetailServiceImpl.class);
    
	@Autowired
	private PurchaseOrderDetailQueryMapper purchaseOrderDetailQueryMapper;
	
	@Autowired
	private MaterialQueryMapper materialMapper;
	
	@Autowired
	private PurchaseOrderDetailMapper purchaseOrderDetailMapper;
	
	@Autowired
	private CustomerSupplierStatementQueryMapper customerSupplierStatementQueryMapper;
	
	@Autowired
	private SupplierCustomerMapper supplierCustomerMapper;
	
	@Autowired
	private PurchaseOrderMapper purchaseOrderMapper;
	
	@Override
	public List<PurchaseOrderDetailInfo> selectDetailBy(PurchaseOrderDetailQueryInfo info) {
		return purchaseOrderDetailQueryMapper.selectById(info);
	}

	@Transactional
    @Override
    public Response<String> edit(PurchaseOrderDetailEditInfo info)
    {
	    logger.debug("编辑采购订单详情提交数据为空{}",info);
	    List<PurchaseOrderDetailEditItemInfo> details = info.getItems();
	    if(CollectionUtils.isEmpty(details)) {
	        logger.error("编辑采购订单详情提交数据为空");
	        return new Response<>(ResultEnum.ERROR);
	    }
	    //int taxPrice=0,noTaxPrice=0,tax=0;
	    List<PurchaseOrderDetail> adddetails = new ArrayList<PurchaseOrderDetail>();//新增的采购订单详情
	    List<PurchaseOrderDetail> editDetails = new ArrayList<PurchaseOrderDetail>();//修改的采购订单详情
	    for(PurchaseOrderDetailEditItemInfo d:details) {
	        PurchaseOrderDetail detail = new PurchaseOrderDetail();
	        if(!StringUtil.isEmpty(d.getId())) {
	            detail = purchaseOrderDetailMapper.selectByPrimaryKey(d.getId());
	            if(detail==null) {
	                logger.error("编辑采购订单明细时,根据id,没找到相应的明细数据，可能改明细不存在",d.getId());
	                throw new RuntimeException("编辑采购订单明细时,根据id,没找到相应的明细数据，可能改明细不存在");
	            }
	            editDetails.add(detail);
	        }else {
	            detail.setRequireTime(new Date());
	            adddetails.add(detail);
	        }
	        detail.setBillId(info.getBillid());
	        detail.setBillNo(info.getBillno());
	        detail.setMaterialId(d.getMaterialId());
	        detail.setStoreageId(d.getWharehouseId());
	        MaterialInfo material = materialMapper.selectById(d.getMaterialId());
	        if(material==null) {
	            logger.error("物料id，根据物料id：{},没找到对应的物料信息");
	            throw new RuntimeException("物料id错误");
	        }
	        //物料有关设置
	        detail.setMaterialCoding(material.getCode());
	        detail.setMaterialSpec(material.getSpec());
	        detail.setMaterialTypeId(material.getTypeid());
	        detail.setMaterialName(material.getName());
	        detail.setMeasurementUId(material.getMearsureUintid());
	        detail.setsMaterialCoding(material.getAliasCode());
	        detail.setMaterialAliasId(material.getAliasId());
	        detail.setsMaterialName(material.getAliasName());
	        detail.setsMaterialSpec(material.getAliasSpec());
	        detail.setMaterialName(material.getName());
	        detail.setBrandId(material.getBrandid());
	        
	        detail.setPrice(d.getPrice());
	        detail.setAccount(d.getNum());
	    }
	    int count = 0;
	    if(!CollectionUtils.isEmpty(adddetails)) {
	        count = purchaseOrderDetailMapper.insertList(adddetails);
	        if(count!=adddetails.size()) {
	            logger.error("编辑采购订单明细错误，更新数据不对updatecount={},提交新增数据条数：{}",count,adddetails.size());
	            throw new RuntimeException("编辑采购订单明细错误，更新数据不对updatecount="+count);
	        }
	    }
	    
	    count = 0;
	    for(PurchaseOrderDetail p:editDetails) {
	        count += purchaseOrderDetailMapper.updateByPrimaryKey(p);
	    }
	    if(count!=editDetails.size()) {
            logger.error("编辑采购订单明细错误，更新数据不对updatecount={},提交修改数据条数：{}",count,editDetails.size());
            throw new RuntimeException("编辑采购订单明细错误，更新数据不对updatecount="+count);
        }
        return new Response<String>(ResultEnum.SUCCESS);
    }

    @Override
    public Response<String> remove(List<Integer> ids)
    {
        int counts = purchaseOrderDetailMapper.deleteByIds(ids);
        //更新purchaseorder 里面统计信息
        
        if(counts!=ids.size()) {
            logger.error("编辑采购订单明细错误，删除数据不对deletecount={},提交修改数据条数：{}",counts,ids.size());
            throw new RuntimeException("删除采购订单明细错误，更新数据不对deletecount="+counts);
        }
        return new Response<String>(ResultEnum.SUCCESS);
    }

}
